package myjava.util;


import java.util.*;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-07-03
 */
public class MyArrayList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable {

    private Object [] table ;

    private int size=0;

    public MyArrayList() {
        table = new Object[16];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean contains(Object o) {
        boolean contain=false;
        for(java.lang.Object obj:table){
            if(obj!=null&&obj.equals(o)){
                contain=true;
                break;
            }
        }
        return contain;
    }

    @Override
    public Iterator<E> iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator{
        int next=0;
        @Override
        public boolean hasNext() {
            return next<size;
        }

        @Override
        public E next() {
            return (E)table[next++];
        }
    }

    @Override
    public Object[] toArray() {
        Object[] objs=new Object[size];
        int index=0;
        for(Object e:table){
            objs[index]=e;
            index++;
        }
        return objs;
    }

    @Override
    public <T> T[] toArray(T[] a) {
        int index=0;
        for(Object e:table){
            a[index]=(T)e;
            index++;
        }
        return a;
    }

    @Override
    public boolean add(E e) {
        if(size+1>=table.length){
            Object[] n=new Object[(int)(table.length*1.5)];
            System.arraycopy(table,0,n,0,table.length);
            this.table=n;
        }
        table[size]=(Object) e;
        size++;
        return true;
    }

    @Override
    public boolean remove(Object o) {
        int index=0;
        for(int i=0;i<size;i++){
            if(table[i].equals(o)){
                index=i;
                break;
            }
        }
        for(int i=index;i<size;i++){
            table[i]=table[i+1];
        }
        size--;
        return true;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        return (E)this.table[index];
    }

    @Override
    public E set(int index, E element) {
        E removed=(E) this.table[index];
        this.table[index]=(Object)element;
        return removed;
    }

    @Override
    public void add(int index, E element) {

    }

    @Override
    public E remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return null;
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }
}
